import random
import time
def shopping_system():
    juan = [
        {"type": "空调", "discount": 8},
        {"type": "苹果", "discount": 8},
        {"type": "苹果", "discount": 5},
        {"type": "香蕉", "discount": 5},
        {"type": "苹果", "discount": 7},
        {"type": "香蕉", "discount": 8},
        {"type": "面包", "discount": 6},
        {"type": "面包", "discount": 8},
        {"type": "牛奶", "discount": 5},
        {"type": "可乐", "discount": 7}
    ]#优惠劵
    chouqu = random.choice(list(juan))#抽取优惠券
    print(f"您抽中的优惠劵为:{chouqu['type']}{chouqu['discount']}折优惠券")
    yuan_money=50.0
    money = 50.0#钱
    products = [
        {"name": "苹果", "price": 2.0},
        {"name": "香蕉", "price": 2.5},
        {"name": "牛奶", "price": 4.0},
        {"name": "面包", "price": 8.5},
        {"name": "可乐", "price": 4.0}
    ]#商品
    cart = []#购物车
#购物
    print("可选购的商品：", [p["name"] for p in products])
    print(f"您的账户余额为：{money}元")
    print("输入商品名称购买，输入Q/q退出")
    n=0#购物数量
    while True:
        guke = input("请输入操作（商品名称/Q/q）: ")#顾客
        if guke.upper() == "Q":#退出
            break
        xuangou = None#选购
        for p in products:#有商品
            if p["name"] == guke:
                xuangou = p
                n+=1
                break
        if not xuangou:#无商品
            print("没有这个商品，别瞎弄！")
            continue
        if money < xuangou["price"]:#钱不够
            print("穷鬼，钱不够！请到其他超市购买！")
            continue

        cart.append(xuangou)#添加到购物车
        print(f"恭喜，购买成功！已将{xuangou['name']}加入购物车")

        if chouqu['type']==xuangou['name']:
            money -= xuangou["price"]*0.1*chouqu['discount']
        else:
            money -= xuangou["price"]#余额
        print(f"您的卡余额还剩：{money:.2f}元")
        #退货
        while True:
            guke_tuihuo = input("想要退货(输入W/w)，不想退货(输入随意)：")
            if guke_tuihuo.upper() == "W":
                tuihuo = input("请输入要退货的商品：")  # tuihuo==name

                found=True
                for i,p in enumerate(cart):
                    if p['name']==tuihuo:
                        tuihuo_value = p['price']
                        if chouqu['type'] == tuihuo:
                            tuihuo_value*=0.1*chouqu['discount']
                        money+=tuihuo_value
                        del cart[i]
                        n-=1
                        print(f"已成功退货 {tuihuo}，退款 {tuihuo_value:.2f} 元")
                        found = True
                        break
                    if not found:
                        print("购物车中没有该商品！")
            else:
                break

    print("\n=====购物小票=====")
    print("商品列表：")
    huaqian = yuan_money-money
    for e in cart:#打印小票
        if chouqu['type'] == e['name']:
            print(f"{e['name']}: {e['price']*0.1*chouqu['discount']:.2f}元")
        else:
            print(f"{e['name']}: {e['price']:.2f}元")
    print(f"购物数量：{n}")
    print(f"总消费：{huaqian:.2f}元")
    print(f"账户余额：{money:.2f}元")
    t=time.localtime()
    print(f"购物时间：{t.tm_year}年{t.tm_mon}月{t.tm_mday}日{t.tm_hour}点{t.tm_min}分{t.tm_sec}秒")
shopping_system()